package com.bookhouse.controller;

import com.bookhouse.constant.ApplicationConstants;
import com.bookhouse.entity.Book;
import com.bookhouse.entity.Reader;
import com.bookhouse.entity.Record;
import com.bookhouse.service.BookService;
import com.bookhouse.service.ReaderService;
import com.bookhouse.service.RecordService;
import com.bookhouse.util.GetTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by CiusCheung on 16/4/22.
 */
@Controller
@RequestMapping("/record")
public class RecordController {
    @Resource(name = "recordService")
    private RecordService recordService;
    @Resource(name = "readerService")
    private ReaderService readerService;
    @Resource(name = "bookService")
    private BookService bookService;

    Record record = new Record();
    Reader reader = new Reader();
    Book book = new Book();

    @RequestMapping("addRecord")
    public String addRecord(Record record) {
//        if (record.getId() != null && record.getId() > 0)
//            recordService.update(record);
//        else {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        record.setCreateTime(timestamp);
        record.setUpdateTime(timestamp);
        record.setIsReturn(ApplicationConstants.RECORD_NOT_RETURNED);
        recordService.save(record);
        book = bookService.get(Book.class, record.getBook().getId());
        int amount = book.getAmount();
        amount--;
        book.setAmount(amount);
        bookService.update(book);
        return "redirect:getAllRecord";
    }

    @RequestMapping("getRecordList")
    public String getRecordList(HttpServletRequest request) {
        List<Record> recordList = recordService.findAllList(Record.class);
        getTimeString(recordList);
        request.setAttribute("recordList", recordList);
        return "recordList";
    }
    
    @RequestMapping("getAllRecord")
    public String getAllRecord(HttpServletRequest request) {
        List<Record> recordList = recordService.findAllList(Record.class);
        getTimeString(recordList);
        request.setAttribute("recordList", recordList);
        return "record";
    }

    @RequestMapping(value = "recordByReader", method = RequestMethod.POST)
    public String recordByReader(String readerId, HttpServletRequest request) {
        String hql = "from Record r where r.reader.id = " + Integer.parseInt(readerId);
        List<Record> recordList = recordService.find(hql);
        getTimeString(recordList);
        request.setAttribute("recordList", recordList);
        return "redirect:getAllRecord";
    }

    @RequestMapping(value = "recordByBook", method = RequestMethod.POST)
    public String recordByBook(String serialNumber, HttpServletRequest request) {
        String hql = "from Record r where r.book.serialNumber = '" + serialNumber + "'";
        List<Record> recordList = recordService.find(hql);
        getTimeString(recordList);
        request.setAttribute("recordList", recordList);
        return "redirect:getAllRecord";
    }


    @RequestMapping("returnBook")
    public String returnBook(String recordId, String bookId, String amount) {
        int number = Integer.parseInt(amount);
        number++;
        record = recordService.get(Record.class, Integer.parseInt(recordId));
        record.setIsReturn(ApplicationConstants.RECORD_RETURNED);
        recordService.update(record);
        book = bookService.get(Book.class, Integer.parseInt(bookId));
        book.setAmount(number);
        bookService.update(book);
        return "redirect:getAllRecord";
    }

    @RequestMapping("continueBook")
    public String continueBook(String recordId, String readerId) {
//        record = recordService.get(Record.class, Integer.parseInt(recordId));
        reader = readerService.get(Reader.class, Integer.parseInt(readerId));
//        long returnTime = GetTime.getReturnDateLong(record.getUpdateTime(),
//                reader.getReaderType().getDaymax());
//        Timestamp returnTime1 = GetTime.getTimestamp(returnTime);
        long updateTime1 = GetTime.getReturnDateLong(record.getUpdateTime(), reader.getReaderType().getContinueday());
        Timestamp updateTime = GetTime.getTimestamp(updateTime1);
        record.setUpdateTime(updateTime);
        recordService.update(record);
        return "redirect:getAllRecord";
    }

    private void getTimeString(List<Record> list) {
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            reader = readerService.get(Reader.class, record.getReader().getId());
            String createDate = GetTime.getTimeStampNumberFormat1(record
                    .getCreateTime());
            String returnTime = GetTime.getReturnDate(record.getUpdateTime(),
                    reader.getReaderType().getDaymax());
            list.get(i).setCreateTime1(createDate);
            list.get(i).setReturnTime(returnTime);
        }
    }

}
